package com.liujun.spring4.demo.aop.boot.web;

import com.liujun.spring4.demo.aop.boot.domain.User;
import com.liujun.spring4.demo.aop.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 登录控制
 * @author liujun
 * @version 1.0
 * @date 2018-08-07 21:54:00
 */
@RestController
public class LoginController {

	/**
	 * 用户服务
	 */
	private UserService userService;

	@RequestMapping(value = {"/","/index.html"})//可以配制多个映射
	public ModelAndView loginPage()
	{
		return new ModelAndView("login");
	}

	@RequestMapping(value = "/loginCheck.html")
	public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand)
	{
		boolean isValidUser =  userService.hasMatchUser(loginCommand.getUserName(),
				loginCommand.getPassword());
		if (!isValidUser) {
			return new ModelAndView("login", "error", "用户名或密码错误。");
		} else {
			User user = userService.findUserByUserName(loginCommand
					.getUserName());
			user.setLastIp(request.getLocalAddr());
			user.setLastVisit(new Date());
			userService.loginSuccess(user);
			request.getSession().setAttribute("user", user);
			return new ModelAndView("main");
		}
	}


	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
